package steveshrader.budget.server;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import com.google.appengine.api.NamespaceManager;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserServiceFactory;

public class NamespaceFilter implements javax.servlet.Filter {
	@Override
	public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
		String namespace = "VERSION-2";
		User user = UserServiceFactory.getUserService().getCurrentUser();
		if(user != null) {	//nobody logged in
			namespace = namespace + "_USER-" + user.getUserId();
		}
		NamespaceManager.set(namespace);
		
		chain.doFilter(req, resp);
	}

	@Override	
	public void init(FilterConfig config) {
	}
	
	@Override	
	public void destroy() {
	}
}
